1 00:00:00,560 --> 00:00:01,580 Welcome back. 2 00:00:01,610 --> 00:00:06,410 Sometimes when we're making games in Roblox, we would like to be able to listen for when players or 3 00:00:06,410 --> 00:00:08,600 parts enter specific zones. 4 00:00:08,600 --> 00:00:12,500 Or we would like to be able to check for any players or parts in a specific region. 5 00:00:12,500 --> 00:00:18,170 In our game, there are Roblox API functions that exist for these purposes, but they can be difficult 6 00:00:18,170 --> 00:00:20,300 to understand or set up on our own. 7 00:00:20,300 --> 00:00:25,610 That's why another developer created a module script that allows us to easily set up zones in our game, 8 00:00:25,610 --> 00:00:30,710 where we can listen for when players or parts enter them, or check for any parts within that zone. 9 00:00:31,100 --> 00:00:36,470 This module is called Zone Plus, and I've attached this dev forum post to the lectures so you can go 10 00:00:36,470 --> 00:00:40,250 ahead and grab the module yourself to start using it in your games. 11 00:00:40,760 --> 00:00:46,580 If we take a look at the documentation for Zone Plus, there are a lot of events, functions and properties 12 00:00:46,580 --> 00:00:49,610 that we can use to set up zones in our game. 13 00:00:49,790 --> 00:00:55,400 One example application of Zone Plus is going to be for a sliding door in our game, where we would 14 00:00:55,400 --> 00:01:00,710 like it to open automatically when a player gets nearby and then close automatically when there are 15 00:01:00,710 --> 00:01:01,970 no players near it. 16 00:01:02,730 --> 00:01:04,200 This is our model right here. 17 00:01:04,200 --> 00:01:06,870 And I've tagged it with the tag of sliding door. 18 00:01:06,870 --> 00:01:11,040 And it's a very basic door system where we just have a part that is going to be our door. 19 00:01:11,040 --> 00:01:13,800 We have this detect part, which is going to be the zone. 20 00:01:13,800 --> 00:01:15,690 We're going to check for players. 21 00:01:15,690 --> 00:01:18,930 And then we have another small part where this is going to be the position. 22 00:01:18,930 --> 00:01:24,570 We're going to set the door when we want to open, and then it'll close and go back to its original 23 00:01:24,570 --> 00:01:26,760 state when there are no players nearby. 24 00:01:27,590 --> 00:01:32,270 I've created this very simple script inside of server script service, where it just goes through and 25 00:01:32,270 --> 00:01:37,400 gets all of the models in our game that have this particular tag of sliding door. 26 00:01:37,400 --> 00:01:41,840 And then we use a class to create an object for that door. 27 00:01:41,870 --> 00:01:47,600 If we go ahead and hop into this class, it's a very basic class where we just pass the model, it sets 28 00:01:47,600 --> 00:01:54,050 up the self table for it, and then we also have a cleanup function to destroy the object when the door 29 00:01:54,050 --> 00:01:56,750 itself is destroyed and stuff like that. 30 00:01:57,610 --> 00:02:04,090 However, now we want to set up this class to go ahead and listen for when players enter into the zone 31 00:02:04,090 --> 00:02:04,930 of our door. 32 00:02:04,930 --> 00:02:08,380 Leave the zone of our door and we want to open and close that door. 33 00:02:08,380 --> 00:02:12,610 So let's go ahead and first get the module script for Zone Plus. 34 00:02:12,610 --> 00:02:16,900 I've placed it as a child of the sliding door class down here. 35 00:02:16,900 --> 00:02:20,830 This is my zone module script and I'll just call it zone up here. 36 00:02:20,830 --> 00:02:23,980 And we'll require script dot zone. 37 00:02:24,070 --> 00:02:27,760 And then I also have my better tween system that we've created earlier. 38 00:02:27,760 --> 00:02:33,760 If you remember in our previous project to allow us to create tweens on the server and then have those 39 00:02:33,760 --> 00:02:35,530 tweens played on the client. 40 00:02:35,530 --> 00:02:36,880 So that way it looks very smooth. 41 00:02:36,880 --> 00:02:38,500 So I'm going to get that as well. 42 00:02:39,150 --> 00:02:44,730 Better tween and will require it from script dot better tween. 43 00:02:45,890 --> 00:02:50,600 Now when our constructor gets called, we'll go ahead and add some properties to our table. 44 00:02:50,600 --> 00:02:53,960 For example, we'll create a reference to the model itself. 45 00:02:53,960 --> 00:02:58,130 We'll create a reference to the door which is going to be model dot door. 46 00:02:58,130 --> 00:03:01,400 And then we're also going to get a reference to that zone. 47 00:03:01,400 --> 00:03:06,740 But we want to go ahead and create a zone object from our zone class. 48 00:03:06,740 --> 00:03:11,630 So I'm going to call this property uh detect zone. 49 00:03:11,930 --> 00:03:14,240 And it's going to be equal to zone. 50 00:03:14,240 --> 00:03:18,380 And then we use the dot new constructor to create a new zone. 51 00:03:18,380 --> 00:03:23,990 And what we pass here is going to be an object or multiple objects that are going to be acting as the 52 00:03:23,990 --> 00:03:25,670 parts for this zone. 53 00:03:25,670 --> 00:03:30,680 In that case, it's going to be my detect part right here in my door model. 54 00:03:30,680 --> 00:03:35,630 So we can go ahead and pass model dot detect part. 55 00:03:35,750 --> 00:03:40,580 And now we're going to get a zone object that has all of those events, functions and properties that 56 00:03:40,580 --> 00:03:42,050 we can go ahead and listen to. 57 00:03:42,650 --> 00:03:43,010 Now. 58 00:03:43,010 --> 00:03:46,280 I also want to be able to keep track of when my door is opened or closed. 59 00:03:46,280 --> 00:03:51,980 So I'll create a boolean, I'll call it is open and we're going to set it to false by default because 60 00:03:51,980 --> 00:03:54,230 all of our doors are currently closed. 61 00:03:54,650 --> 00:03:56,900 And then I want to create another two properties. 62 00:03:56,900 --> 00:04:00,890 One is going to store the open sea frame and the close sea frame for the door. 63 00:04:00,890 --> 00:04:03,230 So we'll call the first one close sea frame. 64 00:04:03,230 --> 00:04:08,150 And that's just equal to our self dot door dot sea frame. 65 00:04:08,330 --> 00:04:17,570 And then for the open sea frame that's going to be equal to our self dot model dot open position that 66 00:04:17,570 --> 00:04:17,900 part. 67 00:04:17,900 --> 00:04:20,390 And then we're going to get that part sea frame. 68 00:04:20,750 --> 00:04:26,360 Now we can go ahead and listen to two events within our detect zone object. 69 00:04:26,360 --> 00:04:31,460 The first event in our detect zone object is called a player entered. 70 00:04:31,460 --> 00:04:33,980 And we can go ahead and connect a function to this. 71 00:04:33,980 --> 00:04:39,080 This event is going to fire any time a player enters into our zone. 72 00:04:39,080 --> 00:04:43,880 So when a player enters into our zone, we would like to open the door. 73 00:04:43,880 --> 00:04:49,310 So let's go ahead and create two functions that are going to belong to the meta table. 74 00:04:50,400 --> 00:04:55,920 One of these functions is going to be for opening the door, and then we'll have another function for 75 00:04:55,920 --> 00:04:57,270 closing the door. 76 00:04:58,690 --> 00:05:02,770 When we want to open the door, we first want to check whether or not the door is open. 77 00:05:02,770 --> 00:05:07,600 So if our door is already opened, then we're just going to return because we don't care. 78 00:05:07,630 --> 00:05:10,540 Otherwise we're going to set is open equal to true. 79 00:05:10,540 --> 00:05:14,260 And then we can use our better tween module and create a new tween. 80 00:05:14,350 --> 00:05:17,050 And that's going to be on self dot door. 81 00:05:17,740 --> 00:05:19,510 We'll do a new tween info. 82 00:05:19,540 --> 00:05:21,340 We'll tween it for like 0.5 seconds. 83 00:05:21,340 --> 00:05:24,070 And then we'll do an enemy's style of sign. 84 00:05:24,400 --> 00:05:30,430 And then we can go ahead and change the key frame of our door to be equal to that self dot opened key 85 00:05:30,430 --> 00:05:31,120 frame. 86 00:05:32,350 --> 00:05:37,900 Then we can basically just copy this and do the exact same thing for our close function. 87 00:05:37,900 --> 00:05:41,260 But this time we want to check if our door is already closed. 88 00:05:41,260 --> 00:05:44,770 So if it's not open then we can just go ahead and return. 89 00:05:44,770 --> 00:05:47,290 Otherwise we'll set is open equal to false. 90 00:05:47,290 --> 00:05:51,940 And this time we want to set the key frame equal to the closed key frame. 91 00:05:52,980 --> 00:05:59,580 Now, what we can go ahead and do is we can listen for when a player enters that zone around our door. 92 00:05:59,580 --> 00:06:04,920 And if a player does enter that zone around our door, we want to go ahead and open the door, right. 93 00:06:05,550 --> 00:06:10,890 So now let's go ahead and test this out and see if our door opens when we get close to it. 94 00:06:13,640 --> 00:06:19,370 So if I go and select my zone here, as soon as my player's character gets into this zone, my door 95 00:06:19,370 --> 00:06:21,590 should open and there we go. 96 00:06:21,590 --> 00:06:24,320 That event fired and now the door has opened. 97 00:06:24,920 --> 00:06:28,070 Now what we need to do is we need to go ahead and close the door. 98 00:06:28,070 --> 00:06:33,350 When there are no players within this zone or when my player has exited the zone. 99 00:06:34,140 --> 00:06:39,690 And that's what we're going to go ahead and use another event in the detect zone, which is called player 100 00:06:39,690 --> 00:06:40,530 exited. 101 00:06:40,830 --> 00:06:43,170 And we can go ahead and connect a function to this. 102 00:06:43,170 --> 00:06:46,680 Now when a player exits the zone we want to go ahead and close the door. 103 00:06:46,680 --> 00:06:52,050 But we only want to close it if there are no other players within that zone around my door. 104 00:06:52,650 --> 00:06:58,290 Thankfully, our zone object actually has a function where we can go ahead and grab all of the players 105 00:06:58,290 --> 00:06:59,850 that are inside of our zone. 106 00:06:59,850 --> 00:07:06,780 So if we refer to our detect zone, there's a function called Get Players, and this is going to return 107 00:07:06,780 --> 00:07:09,630 an array of all the players that are currently in our zone. 108 00:07:09,630 --> 00:07:12,000 So we can store that in a variable called players. 109 00:07:12,920 --> 00:07:19,670 And we can check if the number of players, or if the number of objects in this array is greater than 110 00:07:19,670 --> 00:07:22,340 or equal to one, meaning there are players in the zone. 111 00:07:22,340 --> 00:07:26,210 If there are, then we're just going to return because we don't want to close the door yet. 112 00:07:26,450 --> 00:07:30,980 Otherwise, if there are no players in the zone, then we can go ahead and just close the door. 113 00:07:32,010 --> 00:07:34,920 If we go and walk over and touch the zone. 114 00:07:34,920 --> 00:07:38,490 As you can see, the door opens and if we exit the zone. 115 00:07:41,020 --> 00:07:43,840 Unfortunately, it's not closing yet. 116 00:07:46,560 --> 00:07:48,750 So let's go ahead and see what we did wrong. 117 00:07:49,480 --> 00:07:54,580 And the reason it's not working is because we accidentally forgot to include a C and our C frame for 118 00:07:54,580 --> 00:07:55,570 the closed C frame. 119 00:07:55,570 --> 00:07:56,710 So fix that. 120 00:07:56,710 --> 00:07:58,690 Let's go ahead and try this out again. 121 00:07:59,920 --> 00:08:03,670 If we go and walk up to our door, as you can see, it opens. 122 00:08:03,670 --> 00:08:06,340 And then once we exit the zone, there we go. 123 00:08:06,340 --> 00:08:07,630 Our door closes. 124 00:08:07,660 --> 00:08:08,590 Very cool. 125 00:08:11,890 --> 00:08:16,900 Now if we have multiple players in our game, if my player approaches the door, as you can see, it 126 00:08:16,900 --> 00:08:17,740 opens. 127 00:08:18,040 --> 00:08:21,010 And then if my other player approaches the door, nothing happens. 128 00:08:21,010 --> 00:08:26,830 But as soon as this player leaves, as you can see, our door still remains open because my player two 129 00:08:26,860 --> 00:08:28,270 is still within that zone. 130 00:08:28,270 --> 00:08:32,050 But once this player leaves as well, now the door closes. 131 00:08:32,620 --> 00:08:38,710 So now we have a very cool automatic sliding door that will only close once players have left the zone 132 00:08:38,710 --> 00:08:43,630 that's nearby to the door, and then it automatically opens once a player enters into that zone. 133 00:08:45,230 --> 00:08:50,450 Of course, the amount of possibilities with Zone Plus is absolutely endless, and you can go ahead 134 00:08:50,450 --> 00:08:53,690 and look through all of the different events for our zones. 135 00:08:53,690 --> 00:08:58,190 You can look at the different properties, as well as the different functions, to allow you to create 136 00:08:58,190 --> 00:08:59,600 basically anything you want. 137 00:08:59,600 --> 00:09:05,360 With these zones, such as ambiance zones, you can listen for maybe one specific NPCs, enter a zone 138 00:09:05,360 --> 00:09:10,100 to hurt them, and a lot more things can be done with this module. 139 00:09:10,220 --> 00:09:14,630 So let's go ahead and take one last quick overview of the documentation. 140 00:09:14,630 --> 00:09:20,090 As you can see with the constructor we pass a container and it says a container is used to define the 141 00:09:20,090 --> 00:09:21,500 boundaries of a zone. 142 00:09:21,500 --> 00:09:27,680 It can be any non base part instance such as a model folder etc. that contains descendant base parts. 143 00:09:27,680 --> 00:09:33,650 Alternatively, a container can be a singular base part instance or a table containing an array of base 144 00:09:33,650 --> 00:09:34,160 parts. 145 00:09:34,160 --> 00:09:35,930 So you can really pass a lot of different things. 146 00:09:35,930 --> 00:09:36,110 Here. 147 00:09:36,110 --> 00:09:40,040 You can pass a folder, a model, an array, or just a part. 148 00:09:40,580 --> 00:09:45,260 We also have the ability to find just the local player within a zone. 149 00:09:45,260 --> 00:09:48,440 If you want to set up Ambiance zone, this is going to be really useful. 150 00:09:48,440 --> 00:09:54,350 This function, as well as the local player entered and the local player exited events. 151 00:09:54,350 --> 00:09:56,720 As you can see, they are marked as client only. 152 00:09:57,600 --> 00:10:01,470 Of course, those other two events we just used player entered and player exited. 153 00:10:01,500 --> 00:10:08,100 They execute once a player enters or exits the zone as well as they pass the player instance that has 154 00:10:08,100 --> 00:10:09,900 entered or exited the zone. 155 00:10:09,930 --> 00:10:13,140 We can also check to see if a part has entered our zone. 156 00:10:13,140 --> 00:10:18,930 We can check when a part has exited, and we can also check to see whether an item has entered or exited 157 00:10:18,930 --> 00:10:19,410 the zone. 158 00:10:19,410 --> 00:10:20,430 What is a item? 159 00:10:20,430 --> 00:10:27,000 Well, an item is a particular thing in your game that you want to track, and we do so by using the 160 00:10:27,000 --> 00:10:30,360 function track item and it says right here. 161 00:10:30,360 --> 00:10:36,330 This is used to detect your own custom instances within zones such as NPCs, and is recommended replacement 162 00:10:36,330 --> 00:10:37,500 for part events. 163 00:10:37,500 --> 00:10:38,670 Slash methods. 164 00:10:38,670 --> 00:10:44,190 An item can be any base part or an NPC that has a humanoid and a humanoid part. 165 00:10:44,190 --> 00:10:49,350 Once they are tracked, it can be listened for with the zone item entered and item exited events. 166 00:10:49,710 --> 00:10:55,290 So if there are some specific things that you want to listen for when exiting or entering a zone, then 167 00:10:55,290 --> 00:10:57,690 you would use this function to track those items. 168 00:10:57,690 --> 00:11:03,900 And if those items enter or exit your zone, then the item entered or the item exited events are going 169 00:11:03,900 --> 00:11:04,890 to fire. 170 00:11:04,920 --> 00:11:05,850 Very cool. 171 00:11:06,960 --> 00:11:08,880 That's all I have for you in this lecture. 172 00:11:08,880 --> 00:11:11,610 I hope you enjoyed and I'll see you in the next one.